home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Add-Ons / MPW / MPW dmake 4.0 / directry.c < prev    next >
Text File  |  1995-09-18  |  8KB  |  277 lines

  1. /* RCS      -- $Header: /u5/dvadura/src/public/dmake/src/mac/RCS/directry.c,v 1.1 1994/10/06 17:42:57 dvadura Exp $
  2. -- SYNOPSIS -- Fake directory and file functions for the Mac
  3. --
  4. -- DESCRIPTION
  5. --  This file contains implementations for some ANSI standard routines dmake
  6. --  uses which are not otherwise available for the mac.
  7. --
  8. --  Assume we are using at least 128K ROMS.
  9. --
  10. -- AUTHOR
  11. --      Dennis Vadura, dvadura@watdragon.uwaterloo.ca
  12. --      CS DEPT, University of Waterloo, Waterloo, Ont., Canada
  13. --
  14. --
  15. -- COPYRIGHT
  16. --      Copyright (c) 1992,1994 by Dennis Vadura.  All rights reserved.
  17. -- 
  18. --      This program is free software; you can redistribute it and/or
  19. --      modify it under the terms of the GNU General Public License
  20. --      (version 1), as published by the Free Software Foundation, and
  21. --      found in the file 'LICENSE' included with this distribution.
  22. -- 
  23. --      This program is distributed in the hope that it will be useful,
  24. --      but WITHOUT ANY WARRANTY; without even the implied warrant of
  25. --      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. --      GNU General Public License for more details.
  27. -- 
  28. --      You should have received a copy of the GNU General Public License
  29. --      along with this program;  if not, write to the Free Software
  30. --      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31. --
  32. -- LOG
  33. --     $Log: directry.c,v $
  34.  * Revision 1.1  1994/10/06  17:42:57  dvadura
  35.  * dmake Release Version 4.0, Initial revision
  36.  *
  37. */
  38.  
  39. #include <Errors.h>
  40. #include <Files.h>
  41. #include <OSUtils.h>
  42. #include <StdLib.h>
  43. #include <Strings.h>
  44. #include "extern.h"
  45.  
  46.  
  47.  
  48. /*
  49.  * Implementation of stat function for dmake on the mac.
  50.  *
  51.  * Many fields aren't filled in, and the times are seconds from 1/1//1904,
  52.  * but it should be enough for dmake (I think we only need st_mtime and
  53.  * st_mode's S_IFDIR set correctly).
  54.  */
  55. PUBLIC int
  56. stat(pPath, pStat)
  57. char *pPath;
  58. struct stat *pStat;
  59. {
  60.     CInfoPBRec infoPB;
  61.     OSErr err;
  62.     int retVal;
  63.  
  64.     pPath = Unix2MacFName (pPath);
  65.  
  66.     infoPB.hFileInfo.ioCompletion = NULL;
  67.     infoPB.hFileInfo.ioNamePtr = c2pstr (pPath);
  68.     infoPB.hFileInfo.ioVRefNum = 0;
  69.     infoPB.hFileInfo.ioFDirIndex = 0;
  70.     infoPB.hFileInfo.ioDirID = 0;
  71.     err = PBGetCatInfo(&infoPB, FALSE);
  72.     p2cstr ((StringPtr) pPath);
  73.  
  74.     if (err == noErr) {
  75.         pStat->st_mtime = (time_t) infoPB.hFileInfo.ioFlMdDat;
  76.         pStat->st_ctime = (time_t) infoPB.hFileInfo.ioFlCrDat;
  77.         pStat->st_mode = S_IREAD | S_IEXEC;
  78.  
  79.         /* If it is a directory ... */
  80.         if (infoPB.hFileInfo.ioFlAttrib & 0x10) {
  81.             pStat->st_size = infoPB.dirInfo.ioDrNmFls;
  82.             pStat->st_mode |= S_IFDIR;
  83.         } else {
  84.             pStat->st_size = infoPB.hFileInfo.ioFlLgLen;
  85.             pStat->st_mode |= S_IFREG;
  86.         } /* if ... else */
  87.  
  88.         /* If it is writeable */
  89.         if ((infoPB.hFileInfo.ioFlAttrib & 0x1) == 0) {
  90.             pStat->st_mode |= S_IWRITE;
  91.         } /* if */
  92.         
  93.         retVal = 0;
  94.  
  95.     } else {
  96.         retVal = -1;
  97.     } /* if ... else */
  98.  
  99.     return (retVal);
  100. } /* PUBLIC int stat () */
  101.  
  102.  
  103.  
  104. /*
  105.  * Return the current working directory, or NULL if there is an error.
  106.  */
  107. PUBLIC char *
  108. getcwd(char *pPath, size_t pathSize)
  109. {
  110.     DirInfo dirInfo;
  111.     OSErr err;
  112.     Str255 dirName;
  113.     char *pBeginName;
  114.     char *pC;
  115.     size_t len;
  116.     size_t spaceForColon;
  117.  
  118.     pPath = Unix2MacFName (pPath);
  119.  
  120.     /* Set up the info for the PBGetCatInfo() calls */
  121.     dirInfo.ioCompletion = NULL;
  122.     dirInfo.ioNamePtr = dirName;
  123.     dirInfo.ioVRefNum = 0;
  124.     dirInfo.ioFDirIndex = -1;
  125.     dirInfo.ioDrDirID = 0;
  126.     pBeginName = pPath + pathSize - 1;
  127.     spaceForColon = 0;  /* Make sure we don't have an end colon on the name */
  128.  
  129.     /*
  130.      * Keep going up the directory path until the end is reached or an error
  131.      * occurs.  Ideally, we would check for errors at every level and stop
  132.      * when we received an fnfErr (File Not Found), but it appears that there
  133.      * are some problems with network volumes.  (During testing, I received
  134.      * a paramErr (No Default Volume) beyond the top level.)  Thus, to keep it
  135.      * simple, I assume any error past the first directory indicates we have
  136.      * seen all directories.
  137.      */
  138.     while (TRUE) {
  139.         err = PBGetCatInfo ((CInfoPBPtr) &dirInfo, FALSE);
  140.         len = ((size_t)(unsigned char) dirName[0]);
  141.         if ((err == noErr) && (len < pBeginName - pPath)) {
  142.             p2cstr (dirName);
  143.             pBeginName -= len + spaceForColon;
  144.             strcpy (pBeginName, dirName);
  145.             /* Note that strcpy() adds the '\0' at the end of
  146.                the first directory for us */
  147.             if (spaceForColon == 1) {
  148.                 pBeginName[len] = ':';
  149.             } else {
  150.                 /* The end of the string shouldn't have a ':' */
  151.                 spaceForColon = 1;
  152.             } /* if */
  153.  
  154.             /* Set up for the next call to PBGetCatInfo() with
  155.                the parent's directory ID */
  156.             dirInfo.ioDrDirID = dirInfo.ioDrParID;
  157.  
  158.         } else if (spaceForColon == 1) {
  159.             /* We got past the top-level directory */
  160.             break;
  161.  
  162.         } else {
  163.             /* We either have an error when looking at the first directory
  164.                or have run out of room. */
  165.             return (NULL);
  166.         } /* if ... elses */
  167.     } /* while */
  168.  
  169.     /* Now copy the directory string to the beginning of the path string.
  170.        (It's possible the directory already starts at the beginning of the
  171.        string, but this is unlikely and doesn't hurt anything if it does,
  172.        so we don't bother to check for it.) */
  173.     pC = pPath;
  174.     while ((*(pC++) = *(pBeginName++)) != '\0')
  175.         ;
  176.  
  177.     return (pPath);
  178. } /* PUBLIC char *getcwd () */
  179.  
  180.  
  181.  
  182. /*
  183.  * Change the directory to a new default directory.
  184.  *
  185.  * Return 0 if successful, or -1 if there is an error.
  186.  */
  187. PUBLIC int
  188. chdir(char *pPath)
  189. {
  190.     WDPBRec WDPB;
  191.     VolumeParam vParam;
  192.     OSErr err;
  193.     int result;
  194.     char *pC;
  195.     char c;
  196.  
  197.     pPath = Unix2MacFName (pPath);
  198.  
  199.     /* Set up the directory */
  200.     c2pstr (pPath);
  201.     WDPB.ioCompletion = NULL;
  202.     WDPB.ioNamePtr = pPath;
  203.     WDPB.ioVRefNum = 0;
  204.     WDPB.ioWDProcID = 0;
  205.     WDPB.ioWDDirID = 0;
  206.     err = PBOpenWD (&WDPB, FALSE);
  207.     /* Restore path to a C-type string in case the caller wants
  208.        to use it after this call. */
  209.     p2cstr (pPath);
  210.     if (err != noErr) {
  211.         return (-1);
  212.     } /* if */
  213.  
  214.     /* Set up the volume if necessary */
  215.     if (*pPath != ':') {
  216.         for (pC = pPath + 1; (*pC != ':') && (*pC != '\0'); ++pC)
  217.             ;
  218.         c = *pC;
  219.         *pC = '\0';
  220.         vParam.ioCompletion = NULL;
  221.         vParam.ioNamePtr = c2pstr (pPath);
  222.         vParam.ioVRefNum = WDPB.ioVRefNum;
  223.         err = PBSetVol ((ParmBlkPtr) &vParam, FALSE);
  224.         p2cstr (pPath);
  225.         *pC = c;
  226.         result = ((err == noErr) ? 0 : -1);
  227.  
  228.     } else {
  229.         result = 0;
  230.     } /* if ... else */
  231.     
  232.     return (result);
  233. } /* PUBLIC int chdir () */
  234.  
  235.  
  236.  
  237. /*
  238.  * Change the modification time for the file to the current time.
  239.  *
  240.  * The normal version of utime can set the modification time to any
  241.  * time, this function aborts the function if this is tried.
  242.  *
  243.  * We return 0 if the modification time was updated and -1 if there
  244.  * was an error.
  245.  */
  246. PUBLIC int
  247. utime(char *pPath, time_t *pTimes)
  248. {
  249.     CInfoPBRec infoPB;
  250.     OSErr err;
  251.  
  252.     pPath = Unix2MacFName (pPath);
  253.  
  254.     if (pTimes != NULL) {
  255.         Fatal ("SUBROUTINE SHORTCOMING: utime cannot take a utimbuf struct");
  256.     } /* if */
  257.  
  258.     /* Get the old info */
  259.     infoPB.hFileInfo.ioCompletion = NULL;
  260.     infoPB.hFileInfo.ioNamePtr = c2pstr (pPath);
  261.     infoPB.hFileInfo.ioVRefNum = 0;
  262.     infoPB.hFileInfo.ioFDirIndex = 0;
  263.     infoPB.hFileInfo.ioDirID = 0;
  264.     err = PBGetCatInfo (&infoPB, FALSE);
  265.     if (err != noErr) {
  266.         p2cstr ((StringPtr) pPath);
  267.         return (-1);
  268.     } /* if */
  269.  
  270.     /* Change the modification time and set the new info */
  271.     GetDateTime (&(infoPB.hFileInfo.ioFlMdDat));
  272.     infoPB.hFileInfo.ioDirID = 0;
  273.     err = PBSetCatInfo (&infoPB, FALSE);
  274.     p2cstr ((StringPtr) pPath);
  275.     return ((err == noErr) ? 0 : -1);
  276. } /* PUBLIC int utime () */
  277.